#include "../../sleek/node/billboard/sprite.h"
#include "player.h"

using namespace sleek;
using namespace core;
using namespace device;

Player::Player(sleek::core::engine::engine *engine) : Sprite(engine)
{
    path = "player";
    type = EET_PLAYER;
    offset = b2Vec2(-20,0);

    current = action = EPK_NOTHINK;
    last = EPA_STAND;

    loadSprite();
    setAnimation(EPA_STAND);
}
Player::~Player()
{
}
void Player::loadSprite()
{
    sleek::core::math::array<sleek::texture::texture*> tmp[EPA_COUNT];
        tmp[EPA_STAND].push_back(texture::LoadTextureFromPng("player/idle/i0.png"));
        tmp[EPA_STAND].push_back(texture::LoadTextureFromPng("player/idle/i1.png"));
        tmp[EPA_STAND].push_back(texture::LoadTextureFromPng("player/idle/i2.png"));
        tmp[EPA_STAND].push_back(texture::LoadTextureFromPng("player/idle/i3.png"));
        tmp[EPA_STAND].push_back(texture::LoadTextureFromPng("player/idle/i4.png"));
        tmp[EPA_STAND].push_back(texture::LoadTextureFromPng("player/idle/i5.png"));
        tmp[EPA_STAND].push_back(texture::LoadTextureFromPng("player/idle/i6.png"));
        tmp[EPA_STAND].push_back(texture::LoadTextureFromPng("player/idle/i7.png"));

        tmp[EPA_RUN].push_back(texture::LoadTextureFromPng("player/run/r0.png"));
        tmp[EPA_RUN].push_back(texture::LoadTextureFromPng("player/run/r1.png"));
        tmp[EPA_RUN].push_back(texture::LoadTextureFromPng("player/run/r2.png"));
        tmp[EPA_RUN].push_back(texture::LoadTextureFromPng("player/run/r3.png"));
        tmp[EPA_RUN].push_back(texture::LoadTextureFromPng("player/run/r4.png"));
        tmp[EPA_RUN].push_back(texture::LoadTextureFromPng("player/run/r5.png"));
        tmp[EPA_RUN].push_back(texture::LoadTextureFromPng("player/run/r6.png"));
        tmp[EPA_RUN].push_back(texture::LoadTextureFromPng("player/run/r7.png"));

//        tmp[EPA_JUMP].push_back(texture::LoadTextureFromPng("player/jump/j0.png"));
//        tmp[EPA_JUMP].push_back(texture::LoadTextureFromPng("player/jump/j1.png"));
        tmp[EPA_JUMP].push_back(texture::LoadTextureFromPng("player/jump/j2.png"));
        tmp[EPA_JUMP].push_back(texture::LoadTextureFromPng("player/jump/j3.png"));

//        tmp[EPA_CROUCH].push_back(texture::LoadTextureFromPng("player/crouch/c0.png"));
        tmp[EPA_CROUCH].push_back(texture::LoadTextureFromPng("player/crouch/c1.png"));

        tmp[EPA_ATK].push_back(texture::LoadTextureFromPng("player/atk/a0.png"));
        tmp[EPA_ATK].push_back(texture::LoadTextureFromPng("player/atk/a1.png"));
        tmp[EPA_ATK].push_back(texture::LoadTextureFromPng("player/atk/a2.png"));
        tmp[EPA_ATK].push_back(texture::LoadTextureFromPng("player/atk/a3.png"));
        tmp[EPA_ATK].push_back(texture::LoadTextureFromPng("player/atk/a4.png"));
        tmp[EPA_ATK].push_back(texture::LoadTextureFromPng("player/atk/a5.png"));
        tmp[EPA_ATK].push_back(texture::LoadTextureFromPng("player/atk/a6.png"));
    for(unsigned char i = 0; i<EPA_COUNT; ++i) sprite.push_back(tmp[i]);
    text = tmp[0][0];
}
bool Player::manage(sleek::core::device::input *a)
{
    current = action;
    if(a->type == EVENT_KEY_UP)
    {
        if(a->key[KEY_LEFT]) action ^= EPK_LEFT;
        if(a->key[KEY_RIGHT]) action ^= EPK_RIGHT;
        if(a->key[KEY_DOWN]) action ^= EPK_CROUCH;
        if(a->key[KEY_UP]) action ^= EPK_JUMP;
        if(a->key[KEY_KEY_A]) action ^= EPK_ATK;
    }
    if(a->type == EVENT_KEY_DOWN)
    {
        if(a->key[KEY_LEFT]) action |= EPK_LEFT;
        if(a->key[KEY_RIGHT]) action |= EPK_RIGHT;
        if(a->key[KEY_DOWN]) action |= EPK_CROUCH;
        if(a->key[KEY_UP]) action |= EPK_JUMP;
        if(a->key[KEY_KEY_A]) action |= EPK_ATK;
    }
    if(current != action) animate();
    return false;
}
bool Player::animate()
{
    if(offset.x > 0) offset.x = 15;
    else if(offset.x<0) offset.x = -15;
    else offset.x = 0;

    int anim = last;
        b2Vec2 vel = phy->GetLinearVelocity();
        if(action & EPK_JUMP)
        {
            offset.y = 20;
            phy->ApplyLinearImpulse(b2Vec2(vel.x, phy->GetMass()*50), phy->GetWorldCenter());
            anim = EPA_JUMP;
        }
        else if(action & EPK_LEFT)
        {
            offset.y = 0;
            offset.x = 25;
            env->setRotation(sleek::core::math::vector3df(0,180,0));
            anim = EPA_RUN;
        }
        else if(action & EPK_RIGHT)
        {
            offset.x = -25;
            offset.y = 0;
            env->setRotation(sleek::core::math::vector3df(0,0,0));
            anim = EPA_RUN;
        }
        else if(action & EPK_CROUCH)
        {
            offset.y = 0;
            phy->ApplyLinearImpulse(b2Vec2(vel.x, -phy->GetMass()*500), phy->GetWorldCenter());
            anim = EPA_CROUCH;
        }
        else
        {
            offset.y = 0;
            phy->SetLinearVelocity(b2Vec2(0,vel.y));
            anim = EPA_STAND;
        }
        if(action & EPK_ATK) anim = EPA_ATK;
        setAnimation(anim);
    return false;
}
void Player::render()
{
    if(!phy) return;
    b2Vec2 vel = phy->GetLinearVelocity();
    if(vel.x < phy->GetMass() * 10)
    {
        if(action & EPK_LEFT)
        {
            phy->ApplyForce(b2Vec2(-phy->GetMass()*200-vel.x ,vel.y), phy->GetWorldCenter());
        }
        if(action & EPK_RIGHT)
        {
            phy->ApplyForce(b2Vec2(phy->GetMass()*200-vel.x,vel.y), phy->GetWorldCenter());
        }
    }
}
